import axios from 'axios'
import {Message} from 'element-ui'

// 定义全局cancel
let cancelToken = axios.CancelToken
let cancelSource = cancelToken.source()

// 请求拦截器
axios.interceptors.request.use(config => {
  // 判断是否存在token，如果存在的话，则每个http header都加上token
  let restToken = localStorage.getItem('restToken')
  if (restToken && restToken !== '') {
    // 添加Authorization
    config.headers.Authorization = `${restToken}`
    // 全局添加cancelToken
    config.cancelToken = cancelSource.token
  } else {
    cancelToken = axios.CancelToken
    cancelSource = cancelToken.source()
  }
  return config
}, error => {
  return Promise.reject(error)
})

// 响应拦截器即异常处理
axios.interceptors.response.use(response => {
  // 判断是否是文件下载
  if (response.data.type !== undefined) {
    return response
  } else {
    // 业务响应返回码处理
    console.log(response.data.code)
    if (response.data.code !== '200') {
      Message.error(response.data.msg)
      return Promise.reject(response.data.data)
    } else {
      if (response.headers.authorization) {
        localStorage.setItem('restToken', response.headers.authorization)
        sessionStorage.setItem('autoToken', response.headers.authorization)
      }
      return response
    }
  }
}, err => {
  // 取消请求的情况下，终端Promise调用链
  if (axios.isCancel(err)) {
    return new Promise(() => {
    })
  } else {
    if (err && err.response) {
      switch (err.response.status) {
        case 400:
          err.message = '错误请求'
          break
        case 401:
          err.message = '未授权，请重新登录'
          break
        case 403:
          err.message = '拒绝访问'
          break
        case 404:
          err.message = '请求错误,未找到该资源'
          break
        case 405:
          err.message = '请求方法未允许'
          break
        case 408:
          err.message = '请求超时'
          break
        case 500:
          err.message = '服务器端出错'
          break
        case 501:
          err.message = '网络未实现'
          break
        case 502:
          err.message = '网络错误'
          break
        case 503:
          err.message = '服务不可用'
          break
        case 504:
          err.message = '网络超时'
          break
        case 505:
          err.message = 'http版本不支持该请求'
          break
        default:
          err.message = `连接错误${err.response.status}`
      }
    } else {
      err.message = '连接到服务器失败'
    }
    Message.error(err.message)
    return Promise.reject(err)
  }
})

// axios.defaults.baseURL = '/api'
axios.defaults.timeout = 90000

// get请求方式
function get (config) {
  console.log('1', config)
  return new Promise((resolve, reject) => {
    axios({
      url: config.url,
      method: 'get',
      data: config.data
    }).then(res => {
      resolve(res.data)
    }).catch(err => {
      reject(err)
    })
  })
}

// post请求方式
function post (config) {
  return new Promise((resolve, reject) => {
    axios({
      url: config.url,
      method: 'post',
      data: config.data
    }).then(res => {
      resolve(res.data)
    }).catch(err => {
      reject(err)
    })
  })
}

function downloadGet (config) {
  return new Promise((resolve, reject) => {
    axios({
      url: config.url,
      method: 'get',
      params: config.data,
      responseType: 'blob'
    }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

// put请求方式
function put (config) {
  return new Promise((resolve, reject) => {
    axios({
      url: config.url,
      method: 'put',
      data: config.data
    }).then(res => {
      resolve(res.data)
    }).catch(err => {
      reject(err)
    })
  })
}

// deleteh请求方式
function deleted (config) {
  return new Promise((resolve, reject) => {
    axios({
      url: config.url,
      method: 'delete',
      params: config.data
    }).then(res => {
      resolve(res.data)
    }).catch(err => {
      reject(err)
    })
  })
}

export default {get, post, downloadGet, put, deleted}
